//
//  RPLaunchScreenViewController.m
//  ShowYork
//
//  Created by hxh on 2020/10/21.
//  Copyright © 2020 hxh. All rights reserved.
//

#import "RPLaunchScreenViewController.h"
#import "DiscoverListViewController.h"
#import "LoginViewController.h"
#import "TabBarViewController.h"
#import "RPLaunchScreenViewCell.h"
#import "XHPageControl.h"
@interface RPLaunchScreenViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,RPLaunchScreenViewCellDelegate,UIScrollViewDelegate>
@property (nonatomic,strong)UICollectionView *collectionView;
@property (nonatomic,strong)XHPageControl *pageControl;
@end

@implementation RPLaunchScreenViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUI];
    
    // Do any additional setup after loading the view.
}

- (void)setUI{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake(kScreenW, kScreenH);
    layout.minimumLineSpacing = .0f;
    layout.minimumInteritemSpacing = .0f;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    self.collectionView.showsVerticalScrollIndicator = NO;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.pagingEnabled = YES;
    self.collectionView.bounces = NO;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.collectionView];
    
    [self.collectionView registerClass:[RPLaunchScreenViewCell class] forCellWithReuseIdentifier:@"RPLaunchScreenViewCell"];
    
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self.view);
    }];

    if (@available(iOS 11.0,*)) {
        self.collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else{
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    self.pageControl = [[XHPageControl alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 30)];
    
    self.pageControl.cl_bottom = kScreenH - 70 - GK_SAVEAREA_BTM;
    self.pageControl.currentColor = [RPAppManager themColor];
    self.pageControl.otherColor = RGBA(204, 204, 204, 1);
    self.pageControl.numberOfPages = 3;
    self.pageControl.currentPage = 0;
    self.pageControl.controlSpacing = 4.0f;
    self.pageControl.controlSize = 6;
    self.pageControl.currentMultiple = 3;
    [self.view addSubview:self.pageControl];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 3;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    RPLaunchScreenViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"RPLaunchScreenViewCell" forIndexPath:indexPath];
    cell.delegate = self;
 
    if (indexPath.item == 0) {
        [cell setScreenMainTitle:@"高考社区，寓教于乐" subTitle:@"分享交流、资讯检索的平台" showButton:NO index:indexPath.item];
    }
    if (indexPath.item == 1) {
        [cell setScreenMainTitle:@"学测结合，即时反馈" subTitle:@"“学-测-练-学”的学习闭环" showButton:NO index:indexPath.item];
    }
    if (indexPath.item == 2) {
        [cell setScreenMainTitle:@"轻量化学习，不枯燥压力小" subTitle:@"知识点详细拆分，利用碎片化学习" showButton:YES index:indexPath.item];
    }
    return cell;
}
#pragma mark - scrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView.contentOffset.x > (kScreenW + kScreenW * 0.5)) {
        self.pageControl.hidden = YES;
    }else{
        self.pageControl.hidden = NO;
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    NSInteger index = scrollView.contentOffset.x/kScreenW;
    self.pageControl.currentPage = index;
}

#pragma mark - RPLaunchScreenViewCellDelegate
- (void)showOther{
    
    [[NSUserDefaults standardUserDefaults]setObject:@1 forKey:@"isFirst"];
    
    DiscoverListViewController *listVC = [[DiscoverListViewController alloc]init];
    NavigationViewController *nav = [[NavigationViewController alloc]initWithRootViewController:listVC];
    [UIApplication sharedApplication].keyWindow.rootViewController = nav;
    
//    TabBarViewController *tabBarVC = [[TabBarViewController alloc]init];
//    [UIApplication sharedApplication].keyWindow.rootViewController = tabBarVC;
}
- (void)showLogin{
    
    [[NSUserDefaults standardUserDefaults]setObject:@1 forKey:@"isFirst"];

    LoginViewController *loginVC = [[LoginViewController alloc]init];
    loginVC.isAppLogin = YES;
    NavigationViewController *nav = [[NavigationViewController alloc]initWithRootViewController:loginVC];
    [UIApplication sharedApplication].keyWindow.rootViewController = nav;
    
    
//    NavigationViewController *nav
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
